package com.uncp.comisiondeadmision.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;

import com.uncp.comisiondeadmision.utils.Util;
import com.uncp.comisiondeadmision.util.entity.EPostulante;

public class DPostulante implements IDPostulante {

	private EntityManagerFactory emf ;
	private EntityManager em;
	
	
	public void setEmf(EntityManagerFactory emf) {
		this.emf = emf;
	}

	@SuppressWarnings("unchecked")
	public List<EPostulante> listarPostuSoliciPendi() throws Exception {
		
		em = emf.createEntityManager();
		List<EPostulante> lista = null;
		String estado = "completado";
		try {
			
			Query consulta = em.createQuery("select p from EPostulante p join " +
					" p.solicitudes s where s.str_estado=:estado");
			consulta.setParameter("estado",estado );
			
			lista = consulta.getResultList();
			
		} catch (Exception e) {
		    e.printStackTrace();
		} finally{
			em.close();
		}
		return lista;
	}

	public EPostulante buscarPostulantePorPK(Integer codigo) throws Exception {
		
		em = emf.createEntityManager();
		EPostulante postulante = null;
		try {
			postulante = em.find(EPostulante.class, codigo);
		} catch (Exception e) {
		e.printStackTrace();
		}finally{
			em.close();
		}	
		return postulante;
		
	}

	public void actualizarEstado(EPostulante entidad) throws Exception {
            
		em = emf.createEntityManager();
		try {
			em.getTransaction().begin();
			em.merge(entidad);
				
		} catch (Exception e) {
			em.getTransaction().rollback();
			e.printStackTrace();
		}finally{
			em.getTransaction().commit();
			em.close();
		}
	}


}
